<?php
namespace App\Http\Controllers\OgreenCard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends BaseController {

    public function login(Request $http)
    {
        $name = $http->input('name');
        $pwd  = $http->input("pwd");
        $error  = "";

        if(strtoupper($http->method()) == "POST" )
        {
            $admin = DB::table('og_admin')->where('name',$name)->where('pwd',md5($pwd))->first();

            if($admin){
                $http->session()->put("manager",$admin);
                //return response()->redirectToRoute("ogreenHome");
                $url = config('app.url').route("ogreenHome",[],false);
                return response()->redirectTo($url);
            }else{
                $error = "账号或密码错误";
            }
        }

        return view("ogreencard/index/login",["name"=>$name,"pwd"=>$pwd,"error"=>$error]);
    }

    public function addUser(Request $request)
    {
        if(strtoupper($request->method()) == "POST")
        {
            $i = $request->all();
            $data  = [
                 "name"     => $i["name"],
                 "real_name"=> $i["real_name"],
                 "role"     => $i["role"],
            ];

            $i["pwd"]? $data["pwd"] = md5($i["pwd"]) : "";
            $data["updated_at"] = self::date_time();

            $db = DB::table("og_admin");
            if($i['id']>0){
                $db->where("id",$i['id'])->update($data);
            }else{
                $data["created_at"] = self::date_time();
                $data["com_id"] = COM_ID;
                $db->insert($data);
            }
            return response(["code"=>200,"msg"=>"新增成功"]);

        }else{

            $id = $request->input("id",0);
            if($id){
                $user = DB::table("og_admin")->find($id);
            }else{
                $user = (object)self::get_empty_arr(["id","name","role",'pwd',"real_name"]);
            }

            $roles  = DB::table("og_role")->get();
            return view("ogreencard/user/addUser",['user'=>$user,"msg"=>"",'roles'=>$roles]);
        }
    }

    public function users()
    {
        $users = DB::table("og_admin as a")
            ->leftJoin("og_role as b",'b.role_id',"=","a.role")
            ->whereNull("a.deleted_at")
            ->where('com_id',COM_ID)
            ->paginate(100);

        return view("ogreencard/user/users",["users"=>$users]);
    }

    public function delUser(Request $request)
    {
        $id = $request->input("id");
        DB::table("og_admin")->where('id',$id)->delete();
        return response(["code"=>200,"msg"=>"success"]);
    }

    public function logout(Request $request)
    {
        $request->session()->forget("manager");
        $url = config('app.url').route("ogreenLogin",[],false);
        return response()->redirectTo($url);
    }

    public function welcome(Request $request)
    {
        return view("ogreencard/user/welcome",["admin"=>$request->session()->get("manager")]);
    }


}